//index.js
var app = getApp()
Page({
  data: {
    hasLink: false,
    allowStatus: false,
    rejectStatus: false,
    relationShip: '' // 
  },
  onLoad(options){
    var parent = JSON.parse(options.user)
    var child = JSON.parse(options.child)
    child.relation = options.relation
    this.setData({
      child,
      parent
    })

    this.getAppyRelation()
  },
  getAppyRelation(){
    var relations = ['', '爸爸', '妈妈', '爷爷', '奶奶', '外公', '外婆', '其他']
    var appyRelation 
    var relationShip = this.data.relationShip
    relations.some((item,index) => {
      if (item == this.data.child.relation) {
        appyRelation = index
        relationShip = item
        return true
      }
    })
    this.setData({
      appyRelation,
      relationShip
    })
  },
  login(e){
    var operate = e.currentTarget.dataset.operate
    var _this = this
    app.xlogin.getUser(function(res) {
      var myInfo = app.isNavigateToLogin();
      if (!myInfo) return;
  
      myInfo = app.isNavigateToBind();
      if (!myInfo) return;
      _this.setData({
        user: myInfo
      })
      // 发起邀请
      _this.inviteGuardian(operate)
    })
  },
  // 用户授权登陆之后，获取用户信息之后，
  // 就发起邀请建立关系接口
  inviteGuardian(operate){
    var form = {
      userId: this.data.child.guardianUserId, // 分享小程序的宝贝的监护人的id
      appyRelation: this.data.appyRelation, // 
      childUserIdOrCode:this.data.child.childId,
      appyUserId: app.userInfo.userId, // 分享小程序的宝贝的监护人的id
      relationType: 1, // 家庭关系
      appyType: 2, // 申请:1,邀请：2
      inviteCodeFlag: 0 // 是否为邀请码关注，1为邀请码，0为其他，默认0
    }
    var _this = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['qrcodeInvite'],
      data: form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        _this.setData({
          inviteInfo: res.data
        })

        _this[operate]()
      },
      fail(err){
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      }
    })
  },
  // 拒绝
  reject(){
    var appyStatus = -1 // 同意
    this.updateInvitation(appyStatus)
  },
  //同意
  allow(e){
    var appyStatus = 1 // 同意
    this.updateInvitation(appyStatus)
  },
  updateInvitation(appyStatus){
    var {id,childUserId,appyUserId,relationType} = this.data.inviteInfo
    
    var form = {
      id,
      childUserId,
      appyUserId,
      relationType, 
      appyRelation: this.data.appyRelation,
      appyStatus
    }
    var that = this
    app.middleware.requestAjax({
      serAddr: app.serAddrs['updateInvitationStatus'],
      data: form,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        var title = appyStatus == -1?'拒绝成功':'同意成功'
        wx.showToast({
          title,
          icon: 'none'
        })
        if (appyStatus == -1) {
          that.setData({
            rejectStatus: true
          })
        } else {
          that.setData({
            hasLink: true,
            allowStatus: true
          })
        }        
      },
      fail(err){
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      }
    })
  },
  navigateToBabyList(){
    wx.switchTab({
      url: app.xhx_route.babyIndex
    })
  }
})
